package cn.tedu.csmall.product.webapi.controller;

import cn.tedu.csmall.common.web.JsonResult;
import cn.tedu.csmall.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.pojo.vo.CategoryDetailsVO;
import cn.tedu.csmall.pojo.vo.CategorySimpleListItemVO;
import cn.tedu.csmall.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/categories", produces = "application/json; charset=utf-8")
public class CategoryController {

    public CategoryController() {
        System.out.println("CategoryController.CategoryController()");
    }

    @Autowired
    private ICategoryService categoryService;

    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Validated CategoryAddNewDTO categoryAddNewDTO) {
        categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok();
    }

    @GetMapping("/list-by-parent")
    public JsonResult<List<CategorySimpleListItemVO>> listByParentId(Long parentId){
        List<CategorySimpleListItemVO> list = categoryService.listByParentId(parentId);
        return JsonResult.ok(list);

    }

    @GetMapping("/{id}") //restful
    public JsonResult<CategoryDetailsVO> getDetailsById(@PathVariable Long id){
        CategoryDetailsVO category = categoryService.getDetailsById(id);
        return JsonResult.ok(category);
    }

    @RequestMapping("/{id}/update-enable")
    public JsonResult<Void> updateEnableById(@PathVariable Long id){
        categoryService.updateEnableById(id);
        return JsonResult.ok();
    }

    @RequestMapping("/{id}/update-disEnable")
    public JsonResult<Void> updateDisEnableById(@PathVariable Long id){
        categoryService.updateDisEnableById(id);
        return JsonResult.ok();
    }

    //隐藏
    @RequestMapping("/{id}/update-isdisplay")
    public JsonResult<Void> updateIsDisplayById(@PathVariable Long id){
        categoryService.updateEnableById(id);
        return JsonResult.ok();
    }

    //显示
    @RequestMapping("/{id}/update-isnotdisplay")
    public JsonResult<Void> updateIsNotDisEnableById(@PathVariable Long id){
        categoryService.updateIsNotDisplayById(id);
        return JsonResult.ok();
    }

    //删除
    @RequestMapping("/{id}/delete-by-id")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    //重建缓存
    @RequestMapping("/cache/rebuild")
    public JsonResult<Void> rebuildCache(){

        categoryService.preloadCache();

        return JsonResult.ok();
    }



}
